package pl.register.android.activities.profile.ui;

import android.content.Context;
import android.view.View;
import android.widget.*;
import com.actionbarsherlock.internal.widget.IcsLinearLayout;
import pl.register.android.R;
import pl.register.android.activities.profile.logic.StudentProfilePresenter;
import pl.register.android.classes.Classes;
import pl.register.android.classes.ClassesTopic;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.TagLesson;
import pl.register.android.widget.button.RatingButton;
import pl.register.android.widget.table.StudentLessonsTableAdapter;
import pl.register.android.widget.textview.VerticalTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-20
 */
public class StudentClassesTableAdapter extends StudentLessonsTableAdapter implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private StudentProfileView studentProfileView;

    private StudentProfilePresenter studentProfilePresenter;

    public StudentClassesTableAdapter(Context context, StudentProfileView studentProfileView, StudentProfilePresenter studentProfilePresenter) {
        super(context);
        this.context = context;
        this.studentProfileView = studentProfileView;
        this.studentProfilePresenter = studentProfilePresenter;
    }

    @Override
    public View getLessonSectionView(View convertView, int tagPosition) {
        ViewSectionHolder holder;
        if (convertView == null) {
            holder = new ViewSectionHolder();
            convertView = inflater.inflate(R.layout.table_section_lessons_with_tag, null);
            holder.llClasses = (LinearLayout) convertView.findViewById(R.id.lesson_columns_container);
            holder.tvTagName = (TextView) convertView.findViewById(R.id.tag_name_text_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewSectionHolder) convertView.getTag();
        }
        fillSection(holder, tagPosition);
        return convertView;
    }

    private void fillSection(ViewSectionHolder holder, int tagPosition) {
        TagLesson tag = getTag(tagPosition);
        List<Lesson> lessons = getLessonsBy(tag);

        if (lessons != null && !lessons.isEmpty()) {
            for (int index = 0; index < lessons.size(); index++) {
                boolean isLastColumn = (index == lessons.size() - 1);
                Lesson lesson = lessons.get(index);
                View columnView = createClassesColumn(lesson.getClasses().size(), isLastColumn);
                if (columnView != null) {
                    ViewColumnHolder columnHolder = (ViewColumnHolder) columnView.getTag();
                    fillColumn(columnHolder, lesson, lesson.getClasses());
                    holder.llClasses.addView(columnView);
                }
            }
            holder.tvTagName.setText(tag.getName());
        } else {
            holder.tvTagName.setVisibility(View.GONE);
        }

    }

    private View createClassesColumn(int amountClasses, boolean itsLastColumn) {
        View columnClasses = null;
        ViewColumnHolder columnHolder = new ViewColumnHolder();

        if (amountClasses == 1) {
            columnClasses = inflater.inflate(R.layout.table_column_lesson, null);
            columnHolder.vtvTopics.add((VerticalTextView) columnClasses.findViewById(R.id.topic_vertical_text_view));
            columnHolder.tvSchedule = ((TextView) columnClasses.findViewById(R.id.schedule_text_view));
            columnHolder.cbPresences.add((CheckBox) columnClasses.findViewById(R.id.lesson_check_box));
            columnHolder.btnRatings.add((RatingButton) columnClasses.findViewById(R.id.rating_button));
            columnClasses.setTag(columnHolder);
        } else if (amountClasses == 2) {
            columnClasses = inflater.inflate(R.layout.table_column_double_lesson, null);
            columnHolder.vtvTopics.add((VerticalTextView) columnClasses.findViewById(R.id.topic_1_vertical_text_view));
            columnHolder.vtvTopics.add((VerticalTextView) columnClasses.findViewById(R.id.topic_2_vertical_text_view));
            columnHolder.tvSchedule = ((TextView) columnClasses.findViewById(R.id.schedule_text_view));
            columnHolder.cbPresences.add((CheckBox) columnClasses.findViewById(R.id.lesson_1_check_box));
            columnHolder.cbPresences.add((CheckBox) columnClasses.findViewById(R.id.lesson_2_check_box));
            columnHolder.btnRatings.add((RatingButton) columnClasses.findViewById(R.id.rating_1_button));
            columnHolder.btnRatings.add((RatingButton) columnClasses.findViewById(R.id.rating_2_button));
            columnClasses.setTag(columnHolder);
        }

        if (columnClasses != null && itsLastColumn) {
            IcsLinearLayout llTopics = (IcsLinearLayout) columnClasses.findViewById(R.id.topics_linear_layout);
            llTopics.setShowDividers(IcsLinearLayout.SHOW_DIVIDER_NONE);
            IcsLinearLayout llSchedule = (IcsLinearLayout) columnClasses.findViewById(R.id.schedule_linear_layout);
            llSchedule.setShowDividers(IcsLinearLayout.SHOW_DIVIDER_NONE);
        }

        return columnClasses;
    }

    private void fillColumn(ViewColumnHolder holder, Lesson lesson, List<Classes> classesList) {
        for (int i = 0; i < classesList.size(); i++) {
            Classes classes = classesList.get(i);

            ClassesTopic classesTopic = classes.getTopic();
            VerticalTextView vtvTopic = holder.vtvTopics.get(i);
            vtvTopic.setText(classesTopic.getTopic());

            TextView tvSchedule = holder.tvSchedule;
            tvSchedule.setText(lesson.getClassesDateString());

            CheckBox cbPresence = holder.cbPresences.get(i);
            cbPresence.setChecked(classes.isPresence());
            cbPresence.setTag(classes);
            cbPresence.setOnCheckedChangeListener(this);

            RatingButton btnRating = holder.btnRatings.get(i);
            btnRating.setRating(classes.getRating());
            btnRating.setTag(classes);
            btnRating.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rating_button:
            case R.id.rating_1_button:
            case R.id.rating_2_button:
                RatingButton btnRating = (RatingButton) view;
                Classes classes = (Classes) view.getTag();
                classes.setRating(btnRating.getRating());
                studentProfilePresenter.saveClasses(classes);
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        switch (compoundButton.getId()) {
            case R.id.lesson_check_box:
            case R.id.lesson_1_check_box:
            case R.id.lesson_2_check_box:
                Classes classes = (Classes) compoundButton.getTag();
                classes.setPresence(isChecked);
                studentProfilePresenter.saveClasses(classes);
                break;
        }
    }

    public static class ViewSectionHolder {
        public TextView tvTagName;
        public LinearLayout llClasses;
    }

    public static class ViewColumnHolder {
        public List<VerticalTextView> vtvTopics = new ArrayList<VerticalTextView>();
        public TextView tvSchedule;
        public List<CheckBox> cbPresences = new ArrayList<CheckBox>();
        public List<RatingButton> btnRatings = new ArrayList<RatingButton>();
    }

}